/*******************************************************************************
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via 
 * e-mail: ralf.vandenhouten@th-wildau.de.
 ******************************************************************************/
package de.thwildau.tm.moses.thrift.util;

import static java.util.Collections.unmodifiableList;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.resource.XtextResource;

import com.google.inject.Singleton;

import de.thwildau.tm.moses.thrift.thrift.Header;
import de.thwildau.tm.moses.thrift.thrift.Include;
import de.thwildau.tm.moses.thrift.thrift.Thrift;
 

/**
 * Utility class to find {@link EObject} elements in a Thrift file.
 *
 * @author Thomas Kistel
 * @version $Revision: 759 $
 */
@Singleton
public class ThriftElementFinder {

    /**
     * Returns the root element of the Thrift file containing the given element.
     * 
     * @param obj the given element.
     * @return the root element of the Thrift file containing the given element.
     */
    public Thrift rootOf(EObject obj) {
        EObject current = obj;
        while (!(current instanceof Thrift))
            current = current.eContainer();
        return (Thrift)current;
    }

    
    /**
     * Returns all the include definitions in the given Thrift.
     * 
     * @param root the given 'Thrift' element.
     * @return all the include definitions in the given Thrift.
     */
    public List<Include> includesIn(Thrift root) {
        List<Include> imports = new ArrayList<Include>();
        for (Header e : root.getHeaders()) {
            if (e instanceof Include)
                imports.add((Include)e);
        }
        return unmodifiableList(imports);
    }
    
    /**
     * Returns the root definition of the given Thrift resource. Returns <code>null</code>, if the given
     * <code>resource</code> is not an XtextResource or not a valid Thrift file.
     * 
     * @param resource the EMF Resource
     * @return the Thrift root element of the <code>resource</code> or <code>null</code>
     */
    public Thrift rootElementOf(Resource resource) {
        if (resource instanceof XtextResource) {
            EObject root = ((XtextResource)resource).getParseResult().getRootASTElement();
            return (Thrift)root;
        }

        TreeIterator<Object> contents = EcoreUtil.getAllContents(resource, true);
        if (contents.hasNext()) {
            Object next = contents.next();
            if (next instanceof Thrift)
                return (Thrift)next;
        }
        return null;
    }
}
